package com.hengjun.baccarat.utils;

import java.util.List;

import com.hengjun.baccarat.common.ConfigData;
import com.hengjun.baccarat.pojo.CellObject2;
import com.hengjun.baccarat.pojo.GlobalService;
import com.hengjun.baccarat.pojo.ViewDataObject;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

public class DrawForm extends View {

	private int widthNum = 150;// 列数
	private int heightNum = 6; // 行数
	private int sideSize;// 宽度
	private int screenY;
	private Paint paint;
	private Canvas canvas;

	public DrawForm(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		getScreenPixels();
	}

	private void getScreenPixels() {
		DisplayMetrics dm = new DisplayMetrics();
		dm = getResources().getDisplayMetrics();
		screenY = dm.heightPixels;
		sideSize = ((screenY / 8) * 6 / 6);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		this.canvas = canvas;
		drawForm();
	}

	public void setScreenY(int screenY) {
		this.screenY = screenY;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		setMeasuredDimension(sideSize * widthNum + 2, sideSize * heightNum + 2);
	}

	private void drawForm() {
		paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setAntiAlias(true);
		paint.setStrokeWidth(2);
		paint.setStyle(Style.STROKE);
		float cellX, cellY, cellBX, cellBY;
		for (int i = 0; i < widthNum; i++) {
			for (int j = 0; j < heightNum; j++) {
				cellX = i * sideSize;
				cellY = j * sideSize;
				cellBX = (i + 1) * sideSize;
				cellBY = (j + 1) * sideSize;
				canvas.drawRect(cellX, cellY, cellBX, cellBY, paint);
			}
		}
		paint = new Paint();
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(3);
		paint.setStrokeWidth(1);
		paint.setTextSize(50);
		paint.setStyle(Style.FILL);
		paint.setColor(Color.RED);
		ViewDataObject viewDataObject = ViewDataObject.getViewDataObject();
		List<Object> list = viewDataObject.getBigStreetArrayList();
		for (int i = 0; i < list.size(); i++) {
			@SuppressWarnings("unchecked")
			List<CellObject2> cellList = (List<CellObject2>) list.get(i);
			for (int j = 0; j < cellList.size(); j++) {
				CellObject2 cell2 = cellList.get(j);
				drawBigStreetOval(cell2);
			}
		}

	}

	public void drawBigStreetOval(CellObject2 cellObject2) {
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(3);
		if (cellObject2.getOpenType().equals(ConfigData.BANK_STYLE)) {
			paint.setColor(Color.RED);
			canvas.drawCircle((2 + sideSize) / 2 + cellObject2.getCol()
					* sideSize, sideSize / 2 + sideSize * cellObject2.getRow(),
					sideSize / 2 - 5, paint);
		} else if (cellObject2.getOpenType().equals(
				ConfigData.BANK_DOUBLE_STYLE)) {
			paint.setColor(Color.RED);
			canvas.drawCircle((2 + sideSize) / 2 + cellObject2.getCol()
					* sideSize, sideSize / 2 + sideSize * cellObject2.getRow(),
					sideSize / 2 - 5, paint);
			paint.setStyle(Style.FILL);
			canvas.drawCircle(sideSize / 7 + cellObject2.getCol() * sideSize,
					sideSize / 7 + sideSize * cellObject2.getRow(),
					sideSize / 7, paint);
		} else if (cellObject2.getOpenType().equals(
				ConfigData.BANK_PLAYER_DOUBLE_STYLE)) {
			paint.setColor(Color.RED);
			canvas.drawCircle((2 + sideSize) / 2 + cellObject2.getCol()
					* sideSize, sideSize / 2 + sideSize * cellObject2.getRow(),
					sideSize / 2 - 5, paint);
			paint.setColor(Color.BLUE);
			paint.setStyle(Style.FILL);
			canvas.drawCircle(sideSize - sideSize / 7 + cellObject2.getCol()
					* sideSize, sideSize - sideSize / 7 + sideSize
					* cellObject2.getRow(), sideSize / 7, paint);
		} else if (cellObject2.getOpenType().equals(
				ConfigData.BANK_TWO_DOUBLE_STYLE)) {
			paint.setColor(Color.RED);
			canvas.drawCircle((2 + sideSize) / 2 + cellObject2.getCol()
					* sideSize, sideSize / 2 + sideSize * cellObject2.getRow(),
					sideSize / 2 - 5, paint);
			paint.setStyle(Style.FILL);
			canvas.drawCircle(sideSize / 7 + cellObject2.getCol() * sideSize,
					sideSize / 7 + sideSize * cellObject2.getRow(),
					sideSize / 7, paint);

			paint.setColor(Color.BLUE);
			paint.setStyle(Style.FILL);
			canvas.drawCircle(sideSize - sideSize / 7 + cellObject2.getCol()
					* sideSize, sideSize - sideSize / 7 + sideSize
					* cellObject2.getRow(), sideSize / 7, paint);
		} else if (cellObject2.getOpenType().equals(ConfigData.PLAYERS_STYLE)) {
			paint.setColor(Color.BLUE);
			canvas.drawCircle((2 + sideSize) / 2 + cellObject2.getCol()
					* sideSize, sideSize / 2 + sideSize * cellObject2.getRow(),
					sideSize / 2 - 5, paint);
		} else if (cellObject2.getOpenType().equals(
				ConfigData.PLAYERS_DOUBLE_STYLE)) {
			paint.setColor(Color.BLUE);
			canvas.drawCircle((2 + sideSize) / 2 + cellObject2.getCol()
					* sideSize, sideSize / 2 + sideSize * cellObject2.getRow(),
					sideSize / 2 - 5, paint);

			paint.setColor(Color.BLUE);
			paint.setStyle(Style.FILL);
			canvas.drawCircle(
					sideSize - sideSize / 7 + sideSize * cellObject2.getCol(),
					sideSize - sideSize / 7 + sideSize * cellObject2.getRow(),
					sideSize / 7, paint);
		} else if (cellObject2.getOpenType().equals(
				ConfigData.PLAYERS_BANK_DOUBLE_STYLE)) {

			paint.setColor(Color.BLUE);
			canvas.drawCircle((2 + sideSize) / 2 + cellObject2.getCol()
					* sideSize, sideSize / 2 + sideSize * cellObject2.getRow(),
					sideSize / 2 - 5, paint);
			paint.setColor(Color.RED);
			paint.setStyle(Style.FILL);
			canvas.drawCircle(sideSize / 7 + sideSize * cellObject2.getCol(),
					sideSize / 7 + sideSize * cellObject2.getRow(),
					sideSize / 7, paint);
		} else if (cellObject2.getOpenType().equals(
				ConfigData.PLAYERS_TWO_DOUBLE_STYLE)) {
			paint.setColor(Color.BLUE);
			canvas.drawCircle((2 + sideSize) / 2 + cellObject2.getCol()
					* sideSize, sideSize / 2 + sideSize * cellObject2.getRow(),
					sideSize / 2 - 5, paint);
			paint.setStyle(Style.FILL);
			paint.setColor(Color.RED);
			canvas.drawCircle(sideSize / 7 + sideSize * cellObject2.getCol(),
					sideSize / 7 + sideSize * cellObject2.getRow(),
					sideSize / 7, paint);

			paint.setColor(Color.BLUE);
			paint.setStyle(Style.FILL);
			canvas.drawCircle(
					sideSize - sideSize / 7 + sideSize * cellObject2.getCol(),
					sideSize - sideSize / 7 + sideSize * cellObject2.getRow(),
					sideSize / 7, paint);
			
			
			
		} else if (cellObject2.getOpenType().equals(ConfigData.TIE_STYLE)
				|| cellObject2.getOpenType().equals(
						ConfigData.TIE_PLAYER_DOUBLE_STYLE)
				|| cellObject2.getOpenType().equals(
						ConfigData.TIE_BANK_DOUBLE_STYLE)
				|| cellObject2.getOpenType().equals(
						ConfigData.TIE_TWO_DOUBLE_STYLE)) {

			if (cellObject2.getBeforeType().equals(ConfigData.BANK_STYLE)) {
				paint.setColor(Color.RED);
				canvas.drawCircle((2 + sideSize) / 2 + cellObject2.getCol()
						* sideSize,
						sideSize / 2 + sideSize * cellObject2.getRow(),
						sideSize / 2 - 5, paint);
			} else if (cellObject2.getBeforeType().equals(
					ConfigData.BANK_DOUBLE_STYLE)) {
				paint.setColor(Color.RED);
				canvas.drawCircle((2 + sideSize) / 2 + cellObject2.getCol()
						* sideSize,
						sideSize / 2 + sideSize * cellObject2.getRow(),
						sideSize / 2 - 5, paint);
				paint.setStyle(Style.FILL);
				canvas.drawCircle(sideSize / 7 + cellObject2.getCol()
						* sideSize,
						sideSize / 7 + sideSize * cellObject2.getRow(),
						sideSize / 7, paint);
			} else if (cellObject2.getBeforeType().equals(
					ConfigData.BANK_PLAYER_DOUBLE_STYLE)) {
				paint.setColor(Color.RED);
				canvas.drawCircle((2 + sideSize) / 2 + cellObject2.getCol()
						* sideSize,
						sideSize / 2 + sideSize * cellObject2.getRow(),
						sideSize / 2 - 5, paint);
				paint.setColor(Color.BLUE);
				paint.setStyle(Style.FILL);
				canvas.drawCircle(
						sideSize - sideSize / 7 + cellObject2.getCol()
								* sideSize, sideSize - sideSize / 7 + sideSize
								* cellObject2.getRow(), sideSize / 7, paint);
			} else if (cellObject2.getBeforeType().equals(
					ConfigData.BANK_TWO_DOUBLE_STYLE)) {
				paint.setColor(Color.RED);
				canvas.drawCircle((2 + sideSize) / 2 + cellObject2.getCol()
						* sideSize,
						sideSize / 2 + sideSize * cellObject2.getRow(),
						sideSize / 2 - 5, paint);
				paint.setStyle(Style.FILL);
				canvas.drawCircle(sideSize / 7 + cellObject2.getCol()
						* sideSize,
						sideSize / 7 + sideSize * cellObject2.getRow(),
						sideSize / 7, paint);

				paint.setColor(Color.BLUE);
				paint.setStyle(Style.FILL);
				canvas.drawCircle(
						sideSize - sideSize / 7 + cellObject2.getCol()
								* sideSize, sideSize - sideSize / 7 + sideSize
								* cellObject2.getRow(), sideSize / 7, paint);
			} else if (cellObject2.getBeforeType().equals(
					ConfigData.PLAYERS_STYLE)) {
				paint.setColor(Color.BLUE);
				canvas.drawCircle((2 + sideSize) / 2 + cellObject2.getCol()
						* sideSize,
						sideSize / 2 + sideSize * cellObject2.getRow(),
						sideSize / 2 - 5, paint);
			} else if (cellObject2.getBeforeType().equals(
					ConfigData.PLAYERS_DOUBLE_STYLE)) {
				paint.setColor(Color.BLUE);
				canvas.drawCircle((2 + sideSize) / 2 + cellObject2.getCol()
						* sideSize,
						sideSize / 2 + sideSize * cellObject2.getRow(),
						sideSize / 2 - 5, paint);
				paint.setColor(Color.BLUE);
				paint.setStyle(Style.FILL);
				canvas.drawCircle(
						sideSize - sideSize / 7 + sideSize * cellObject2.getCol(),
						sideSize - sideSize / 7 + sideSize * cellObject2.getRow(),
						sideSize / 7, paint);
			} else if (cellObject2.getBeforeType().equals(
					ConfigData.PLAYERS_BANK_DOUBLE_STYLE)) {
				paint.setColor(Color.BLUE);
				canvas.drawCircle((2 + sideSize) / 2 + cellObject2.getCol()
						* sideSize,
						sideSize / 2 + sideSize * cellObject2.getRow(),
						sideSize / 2 - 5, paint);

				paint.setColor(Color.RED);
				paint.setStyle(Style.FILL);
				canvas.drawCircle(sideSize / 7 + sideSize * cellObject2.getCol(),
						sideSize / 7 + sideSize * cellObject2.getRow(),
						sideSize / 7, paint);
			} else if (cellObject2.getBeforeType().equals(
					ConfigData.PLAYERS_TWO_DOUBLE_STYLE)) {
				paint.setColor(Color.BLUE);
				canvas.drawCircle((2 + sideSize) / 2 + cellObject2.getCol()
						* sideSize,
						sideSize / 2 + sideSize * cellObject2.getRow(),
						sideSize / 2 - 5, paint);
				paint.setColor(Color.RED);
				paint.setStyle(Style.FILL);
				canvas.drawCircle(
						sideSize / 7 + sideSize * cellObject2.getCol(),
						sideSize / 7 + sideSize * cellObject2.getRow(),
						sideSize / 7, paint);

				paint.setColor(Color.BLUE);
				paint.setStyle(Style.FILL);
				canvas.drawCircle(sideSize - sideSize / 7 + sideSize
						* cellObject2.getCol(), sideSize - sideSize / 7
						+ sideSize * cellObject2.getRow(), sideSize / 7, paint);
			}
			
			
		}
		if (cellObject2.getTieContinus().doubleValue() != 0){
			paint.setStrokeWidth(1);
			paint.setTextSize(30);
			paint.setStyle(Style.FILL);
			paint.setColor(Color.RED);
			canvas.drawText(cellObject2.getTieContinus().toString(), sideSize
					/ 10 + sideSize * cellObject2.getCol(), sideSize - sideSize
					/ 9 + sideSize * cellObject2.getRow(), paint);
		}
		
		if (cellObject2.getWinTotal().doubleValue() != 0) {
			paint.setStrokeWidth(1);
			paint.setTextSize(25);
			paint.setStyle(Style.FILL);
			if (cellObject2.getWinTotal() > 0) {
				paint.setColor(Color.BLACK);
			} else {
				paint.setColor(Color.RED);
			}
			canvas.drawText(GlobalService.getDoubleString(Math.abs(cellObject2
					.getWinTotal())),
					2 + sideSize / 8 + sideSize * cellObject2.getCol(), 5
							+ sideSize / 2 + sideSize * cellObject2.getRow(),
					paint);
		}
	}
}
